package widget.common.managers
{	
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	import widget.common.utils.MyLog;
	import widget.common.event.BaseEvent;

	[Event(name="removePanel", type="widget.common.event.BaseEvent")]
	/**
	 * 面板UI管理器，报括添加、删除、模糊、得到已有面板实例的方法
	 * @author zizhen
	 *
	 */
	public class PanelManager implements IManager
	{
		//单例
		private static var instance:PanelManager = null;
		
		public static const TOP_LAYER:int = 1;
		public static const BOTTOM_LAYER:int = 0;
		public static const LAYER_INIT:int = 0;
		
		private var currentPanel:Sprite = null;
	
		private var rootContainer:Sprite = null;
		
		private var nextName:String = "";
		
		private var savedList:Array = [];

		public function PanelManager()
		{
			if(instance)
			{
				throw new Error("single class by EventManager")
			}
			init();
		}
		
		public static function getInstance():PanelManager
		{
			if(!instance)
			{
				instance = new PanelManager();
			}
			return instance;
		}
		
		private function init():void
		{
			
		}
		
		/**
		 * 设置根显示容器
		 * @param root
		 *
		 */
		public function set rootOwner(root:Sprite):void
		{
			rootContainer = root;
		}

		public function get rootOwner():Sprite{
			return rootContainer;
		}
		
		public function moveCenter(target:DisplayObjectContainer):void{
			if(rootContainer){
				target.x = (rootContainer.width - target.width)/2;
				target.y = (rootContainer.height - target.height)/2;
			}
		}
		
		public function addToUIByName(panelName:String):void
		{
			if(currentPanel) {
				if(panelName == currentPanel.name) return;
				nextName = panelName;
				dispearFormUI();
			}else{
				var displayObject:Sprite = panelFactory(panelName);
				addToUI(displayObject);
			}
		}

		/**
		 * 向桌面添加一个面板
		 * @param panel
		 * @param name
		 * @return
		 *
		 */
		private function addToUI(panel:Sprite):void
		{
			if(panel == null) return;
			rootContainer.addChild(panel);
			currentPanel = panel;
		}

		/**
		 * 判断该面板是否已经显示
		 * @param name
		 * @return
		 *
		 */
		public function isDisplaying():Boolean
		{

			if(currentPanel)
				return true;
			else
				return false;
		}

		public function dispearFormUI():Boolean
		{
			if (currentPanel)
			{
				currentPanel.dispatchEvent(new BaseEvent(BaseEvent.DISPEAR_PANEL,currentPanel.name));
				return true;
			}
			return false;
		}
		
		public function removeFromUI(panel:Sprite):void{
			if(currentPanel != panel) return;
			if (currentPanel && rootContainer.contains(currentPanel))
			{
				rootContainer.removeChild(currentPanel);
				currentPanel.dispatchEvent(new BaseEvent(BaseEvent.REMOVE_PANEL,currentPanel.name));
				currentPanel = null;
			}
			if(nextName != ""){
				addToUIByName(nextName);
			}
			nextName = "";
		}
		
		public function getCurrentPanel(name:String):Sprite
		{
			return currentPanel;
		}
		
		public function dispose():void
		{
			currentPanel = null;
			nextName = "";
			rootContainer=null;
		}

		private function panelFactory(className:String):Sprite
		{
			if (!currentPanel)
			{
				try{
					var ClassReference:Class = getDefinitionByName(className)as Class
					currentPanel = new ClassReference();
					currentPanel.name = className;
				}catch(error:ReferenceError){
					throw new Error("factory error!!!! "+error.message);
				}
				
			}
			return currentPanel;
		}
	}
}